
import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import NotFound from '@/views/404.vue'
import { setTitle } from '@/utils'
import { VM } from '../main.js'
import config from '@/config'
import { removePending } from '@/api/request'
const { homePath } = config
const files = require.context('.', false, /\.js$/)
let modules = []

files.keys().forEach(fileName  => {
  if (fileName.indexOf('index.js') ==-1){
    let _fileConfig = files(fileName)
    modules = modules.concat(_fileConfig.default||_fileConfig)
  }
})


const homeRoute = [{path: '/', redirect: homePath}]
 // 未知页面转向首页
const nomatched = [{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound}]
const routes = homeRoute.concat(modules.concat(nomatched))
// console.log(19,routes)

const router = createRouter({
	history: createWebHistory(),
	routes,
  // base: process.env.BASE_URL,
  scrollBehavior(to, from, savedPosition) {
    return {
      el: 'body',
      top: 0,
    }
  },
});

// import NProgress from 'nprogress'
// import 'nprogress/nprogress.css'
// NProgress.configure({ showSpinner: false })

router.beforeEach((to, from) => {
  removePending(from.name, true) //取消from路由页面所有数据请求cacel设置为true的ajax请求
  // NProgress.start()
})

router.afterEach(to => {
  setTitle(to, VM)
  // iView.LoadingBar.finish()
  window.scrollTo(0, 0)
  // NProgress.done()
})

export default router

